package com.algorithm.example.linkedList;

import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * <p>
 * 单项链表
 * </p>
 *
 * @author lzt
 * @version 1.0
 * @since 2024/1/18 15:58
 */
@Component
public class SinglyLinkedLike implements Iterable<Integer> {

    /**
     * 头节点
     */
    private Node head = null;

    private static class Node {
        /**
         * 值
         */
        private int value;

        /**
         * 指向下个一节点
         */
        private Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", next=" + next +
                    '}';
        }
    }

    /**
     * 添加
     */
    public void addFirst(int value) {
        // 同时处理链表空与非空
        head = new Node(value, head);
    }

    /**
     * while遍历
     */
    public void loop1(Consumer<Integer> consumer) {
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * for遍历
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    /**
     * 递归遍历
     */
    public void loop3(Consumer<Integer> consumer) {
        recursion(head,consumer);
    }

    private void recursion(Node node,Consumer<Integer> consumer) {
        if(node == null) {
            return;
        }
        consumer.accept(node.value);
        recursion(node.next,consumer);

    }

    /**
     * 迭代器遍历
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head;

            @Override
            public boolean hasNext() { //是否有下一个
                return p != null;
            }

            @Override
            public Integer next() { // 指向下一个
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    /**
     * 根据索引查询，返回元素值
     */
    public int get(int value) {
        Node node = findNode(value);
        try {
            if (node == null) {
                throw new RuntimeException("出错");
            }
            return node.value;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据索引插入
     */
    public void insert(int index, int value) {
        if (index == 0) { // 处理链表为空时
            addFirst(value);
            return;
        }
        Node node = findNode(index - 1);
        if (node == null) {
            throw new RuntimeException("出错");
        }
        node.next = new Node(value, node.next);
    }

    /**
     * 根据索引值删除元素
     */
    public void remove(int value) {
        if (value == 0) {
            removeFirst();
            return;
        }
        Node finedNode = findNode(value);
        if (finedNode == null) {
            throw new RuntimeException("出错");
        }
        Node node = findNode(value - 1);
        if (node == null) {
            throw new RuntimeException("出错");
        }
        node.next = finedNode.next;

    }

    /**
     * 删除第一个元素
     */
    public void removeFirst() {
        if (head == null) {
            throw new RuntimeException("出错了");
        }
        head = head.next;
    }

    /**
     * 根据索引返回Node
     */
    private Node findNode(int value) {
        Node p;
        int i = 0;
        for (p = head; p != null; p = p.next, i++) {
            if (value == i) {
                return p;
            }
        }
        return null;
    }
//    public int get(int value) {
//        int i = 0;
//        Node node = head;
//        if (head == null) {
//            return -1;
//        }
//        if (head.value != value) {
//            head = head.next;
//            i = get(value) == -1 ? i++ : -1;
//        }
//        head = node;
//        return i;
//    }

    /**
     * 最后节点
     */
    private Node findLast() {
        Node p = head;
        if (p == null) {
            return null;
        }
        while (p != null) {
            p = p.next;
        }
        return p;
    }

    /**
     * 添加元素到最后
     */
    public void addLast(int value) {
        Node p = findLast();
        if (p == null) {
            addFirst(value);
        } else {
            p.next = new Node(value, null);
        }
    }

    public Node getHead() {
        return head;
    }
}
